package model;

import java.awt.Color;
import java.util.Observable;
import java.util.Random;

import view.GamePanel;

public class Shape extends Observable {
	private int[][][] shapes = {

			{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 0, 0 } },
			{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
			{ { 0, 1, 0, 0 }, { 0, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
			{ { 0, 0, 1, 0 }, { 0, 0, 1, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
			{ { 0, 0, 0, 0 }, { 0, 1, 1, 0 }, { 1, 1, 0, 0 }, { 0, 0, 0, 0 } },
			{ { 0, 0, 0, 0 }, { 1, 1, 0, 0 }, { 0, 1, 1, 0 }, { 0, 0, 0, 0 } },
			{ { 0, 0, 0, 0 }, { 0, 1, 0, 0 }, { 1, 1, 1, 0 }, { 0, 0, 0, 0 } } };
	private int currentshape = 0;
	private int nextshape = 0;
	private Oneshape oneshape;
	private Oneshape nshape;
	private int y = 0;
	private int x = 0;
	private Color[] color = { Color.CYAN, Color.GREEN, Color.gray, Color.MAGENTA, Color.BLUE, Color.RED,
			Color.LIGHT_GRAY };
	public static final int RIGHT = 1;
	public static final int LEFT = 3;
	public static final int DOWN = 2;
	private int direction = DOWN;
	private int[][] spe;

	public void MakeShape() {
		x=0;y=0;
		Random rd = new Random();
		currentshape = nextshape;
		nextshape = rd.nextInt(7);
		System.out.println("c=" + currentshape);
		System.out.println("n=" + nextshape);
		nshape.setX(60);
		nshape.setY(70);
		nshape.setShape(shapes[nextshape]);
		System.out.println("n=" + nextshape);
		nshape.setColor(color[nextshape]);
		nshape.setNext(true);
		setChanged();
		notifyObservers(nshape);
		oneshape.setX(160);
		oneshape.setY(0);
		oneshape.setShape(shapes[currentshape]);
		System.out.println("c=" + currentshape);
		oneshape.setColor(color[currentshape]);
		oneshape.setNext(false);
		setChanged();
		notifyObservers(oneshape);
	}

	public int[][] GetCShape() {
		return shapes[currentshape];
	}

	public int[][] GetNShape() {
		return shapes[nextshape];
	}

	public void Init() {
		nextshape = new Random().nextInt(7);
		oneshape = new Oneshape();
		nshape = new Oneshape();
		MakeShape();
		Move();
	}

	public void Move() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					if (oneshape.getY() > 550)
					{
						MakeShape();
					}
					y += GamePanel.HEIGHT;
					oneshape.setY(y);
					setChanged();
					notifyObservers(oneshape);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	public void Rotate() {
		spe = oneshape.getShape();
		for (int i = 0; i < spe.length / 2; i++) {
			for (int j = i; j < spe.length - 1 - i; j++) {
				int t = spe[i][j];
				spe[i][j] = spe[spe.length - j - 1][i];
				spe[spe.length - j - 1][i] = spe[spe.length - 1 - i][spe.length - 1 - j];
				spe[spe.length - 1 - i][spe.length - 1 - j] = spe[j][spe.length - 1 - i];
				spe[j][spe.length - 1 - i] = t;
			}
		}
	}

	public void ChangeDirection(int direction) {
		x = oneshape.getX();
		y = oneshape.getY();
		this.direction = direction;
		switch (this.direction) {
		case LEFT:
			x -= 20;
			oneshape.setX(x);
			break;
		case RIGHT:
			x += 20;
			oneshape.setX(x);
			break;
		case DOWN:
			y += 20;
			oneshape.setY(y);
			break;
		}
	}
}
